package com.example.demo.tree.t001;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeNodeConfig;
import cn.hutool.core.lang.tree.TreeUtil;
import cn.hutool.json.JSONUtil;

import java.util.List;

/**
 * @ClassName Test
 * @Description: TODO
 * @Author Administrator
 * @Date 2021/10/14 23:10
 * @Version V1.0
 **/
public class Test {
    public static void main(String[] args) {
        // 构建node列表
        List<TreeNode> nodeList = CollUtil.newArrayList();
        nodeList.add(new TreeNode(1,0,"系统管理",5));
        nodeList.add(new TreeNode(11, 1, "用户管理", 222222));
        nodeList.add(new TreeNode(111, 11, "用户添加", 0));
        nodeList.add(new TreeNode(2, 0, "店铺管理", 1));
        nodeList.add(new TreeNode(21, 2, "商品管理", 44));
        nodeList.add(new TreeNode(221, 2, "商品管理2", 2));
        nodeList.add(new TreeNode(3, 0, "企业管理", 2));

        //配置
        TreeNodeConfig treeNodeConfig = new TreeNodeConfig();
        // 自定义属性名 都要默认值的
        treeNodeConfig.setWeightKey("order");
        treeNodeConfig.setIdKey("id");
        treeNodeConfig.setNameKey("resourceName");
        // 最大递归深度
        treeNodeConfig.setDeep(1000000000);

        //转换器
        List<Tree<Integer>> treeNodes = TreeUtil.build(nodeList, 0, treeNodeConfig,
                (treeNode, tree) -> {
                    tree.setId(treeNode.getId());
                    tree.setParentId(treeNode.getParentId());
                    tree.setWeight(treeNode.getWeight());
                    tree.setName(treeNode.getResourceName());
                    // 扩展属性 ...
                    tree.putExtra("extraField", 666);
                });
        System.out.println(JSONUtil.toJsonPrettyStr(treeNodes));
    }
}
